using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Collectible : MonoBehaviour
{
	public enum SkinTYPE
	{
		Miner,
		Assistant,
		Transporter
	}

	[Serializable]
	public struct Properties
	{
		public int minerSkin;

		public int assistantSkin;

		public int transporterSkin;

		public List<int> minerUnlockSkin;

		public List<int> assistantUnlockSkin;

		public List<int> transporterUnlockSkin;

		public void Initialize()
		{
			minerUnlockSkin = new List<int>();
			minerUnlockSkin.Add(minerSkin);
			assistantUnlockSkin = new List<int>();
			assistantUnlockSkin.Add(assistantSkin);
			transporterUnlockSkin = new List<int>();
			transporterUnlockSkin.Add(transporterSkin);
		}
	}

	[Serializable]
	public struct SkinData
	{
		public SkinTYPE skinTYPE;

		public Sprite avatar;

		public int skinID;

		public float effective;
	}

	public static Collectible instance;

	private Properties properties;

	private WaitForSeconds waitForSeconds;

	private RectTransform worldRectTransform;

	private bool openCollectible;

	private string animateClip = "Open Collectibles";

	public Action<int> changeMinerSkin;

	public Action<int> changeAssistantSkin;

	public Action<int> changeTransporterSkin;

	[SerializeField]
	private Graphic graphic;

	[SerializeField]
	private WorldMap worldMap;

	[SerializeField]
	private Animation openAnimate;

	[SerializeField]
	private Sprite lockedIcon;

	[SerializeField]
	private SkinData[] skinData;

	[SerializeField]
	private GameObject[] minerSkin;

	[SerializeField]
	private GameObject[] assistantSkin;

	[SerializeField]
	private GameObject[] transporterSkin;

	public Properties Property
	{
		get
		{
			return properties;
		}
		set
		{
			properties = value;
		}
	}

	private void Awake()
	{
		instance = this;
		waitForSeconds = new WaitForSeconds(openAnimate.GetClip(animateClip).length);
		worldRectTransform = openAnimate.gameObject.GetComponent<RectTransform>();
	}

	public void Initialize()
	{
		int k;
		for (k = 0; k < minerSkin.Length; k++)
		{
			if (properties.minerUnlockSkin.Contains(k))
			{
				Image component = minerSkin[k].transform.GetChild(1).GetComponent<Image>();
				SkinData skinData = Array.Find(this.skinData, (SkinData target) => target.skinID == k && target.skinTYPE == SkinTYPE.Miner);
				component.sprite = skinData.avatar;
				minerSkin[k].transform.GetChild(1).GetComponent<Image>().SetNativeSize();
				minerSkin[k].transform.GetChild(0).gameObject.SetActive(value: false);
			}
			else
			{
				minerSkin[k].transform.GetChild(1).GetComponent<Image>().sprite = lockedIcon;
				minerSkin[k].transform.GetChild(1).GetComponent<Image>().SetNativeSize();
			}
			minerSkin[k].transform.GetChild(2).gameObject.SetActive(k == properties.minerSkin);
		}
		int j;
		for (j = 0; j < assistantSkin.Length; j++)
		{
			if (properties.assistantUnlockSkin.Contains(j))
			{
				Image component2 = assistantSkin[j].transform.GetChild(1).GetComponent<Image>();
				SkinData skinData2 = Array.Find(this.skinData, (SkinData target) => target.skinID == j && target.skinTYPE == SkinTYPE.Assistant);
				component2.sprite = skinData2.avatar;
				assistantSkin[j].transform.GetChild(1).GetComponent<Image>().SetNativeSize();
				assistantSkin[j].transform.GetChild(0).gameObject.SetActive(value: false);
			}
			else
			{
				assistantSkin[j].transform.GetChild(1).GetComponent<Image>().sprite = lockedIcon;
				assistantSkin[j].transform.GetChild(1).GetComponent<Image>().SetNativeSize();
			}
			assistantSkin[j].transform.GetChild(2).gameObject.SetActive(j == properties.assistantSkin);
		}
		int i;
		for (i = 0; i < transporterSkin.Length; i++)
		{
			if (properties.transporterUnlockSkin.Contains(i))
			{
				Image component3 = transporterSkin[i].transform.GetChild(1).GetComponent<Image>();
				SkinData skinData3 = Array.Find(this.skinData, (SkinData target) => target.skinID == i && target.skinTYPE == SkinTYPE.Transporter);
				component3.sprite = skinData3.avatar;
				transporterSkin[i].transform.GetChild(1).GetComponent<Image>().SetNativeSize();
				transporterSkin[i].transform.GetChild(0).gameObject.SetActive(value: false);
			}
			else
			{
				transporterSkin[i].transform.GetChild(1).GetComponent<Image>().sprite = lockedIcon;
				transporterSkin[i].transform.GetChild(1).GetComponent<Image>().SetNativeSize();
			}
			transporterSkin[i].transform.GetChild(2).gameObject.SetActive(i == properties.transporterSkin);
		}
	}

	public void SelectMinerSkin(int index)
	{
		if (properties.minerUnlockSkin.Contains(index) && index != properties.minerSkin)
		{
			minerSkin[properties.minerSkin].transform.GetChild(2).gameObject.SetActive(value: false);
			minerSkin[index].transform.GetChild(2).gameObject.SetActive(value: true);
			properties.minerSkin = index;
			if (changeMinerSkin != null)
			{
				changeMinerSkin(index);
			}
			Sound.instance.Play("Equip");
		}
	}

	public void SelectAssistantSkin(int index)
	{
		if (properties.assistantUnlockSkin.Contains(index) && index != properties.assistantSkin)
		{
			assistantSkin[properties.assistantSkin].transform.GetChild(2).gameObject.SetActive(value: false);
			assistantSkin[index].transform.GetChild(2).gameObject.SetActive(value: true);
			properties.assistantSkin = index;
			if (changeAssistantSkin != null)
			{
				changeAssistantSkin(index);
			}
			Sound.instance.Play("Equip");
		}
	}

	public void SelectTransporterSkin(int index)
	{
		if (properties.transporterUnlockSkin.Contains(index) && index != properties.transporterSkin)
		{
			transporterSkin[properties.transporterSkin].transform.GetChild(2).gameObject.SetActive(value: false);
			transporterSkin[index].transform.GetChild(2).gameObject.SetActive(value: true);
			properties.transporterSkin = index;
			if (changeTransporterSkin != null)
			{
				changeTransporterSkin(index);
			}
			Sound.instance.Play("Equip");
		}
	}

	public void UnlockSkin(SkinTYPE skinTYPE, float effective)
	{
		if (skinTYPE != 0)
		{
			if (skinTYPE != SkinTYPE.Assistant)
			{
				if (skinTYPE == SkinTYPE.Transporter)
				{
					List<int> transporterUnlockSkin = properties.transporterUnlockSkin;
					SkinData skinData = Array.Find(this.skinData, (SkinData target) => target.skinTYPE == skinTYPE && target.effective == effective);
					transporterUnlockSkin.Add(skinData.skinID);
				}
			}
			else
			{
				List<int> assistantUnlockSkin = properties.assistantUnlockSkin;
				SkinData skinData2 = Array.Find(this.skinData, (SkinData target) => target.skinTYPE == skinTYPE && target.effective == effective);
				assistantUnlockSkin.Add(skinData2.skinID);
			}
		}
		else
		{
			List<int> minerUnlockSkin = properties.minerUnlockSkin;
			SkinData skinData3 = Array.Find(this.skinData, (SkinData target) => target.skinTYPE == skinTYPE && target.effective == effective);
			minerUnlockSkin.Add(skinData3.skinID);
		}
	}

	public float GetSkinEffective(SkinTYPE skinTYPE)
	{
		int skinID = 0;
		if (skinTYPE != 0)
		{
			if (skinTYPE != SkinTYPE.Assistant)
			{
				if (skinTYPE == SkinTYPE.Transporter)
				{
					skinID = properties.transporterSkin;
				}
			}
			else
			{
				skinID = properties.assistantSkin;
			}
		}
		else
		{
			skinID = properties.minerSkin;
		}
		SkinData skinData = Array.Find(this.skinData, (SkinData target) => target.skinID == skinID && target.skinTYPE == skinTYPE);
		return skinData.effective;
	}

	public Sprite GetSkinIcon(SkinTYPE skinTYPE)
	{
		int skinID = 0;
		if (skinTYPE != 0)
		{
			if (skinTYPE != SkinTYPE.Assistant)
			{
				if (skinTYPE == SkinTYPE.Transporter)
				{
					skinID = properties.transporterSkin;
				}
			}
			else
			{
				skinID = properties.assistantSkin;
			}
		}
		else
		{
			skinID = properties.minerSkin;
		}
		SkinData skinData = Array.Find(this.skinData, (SkinData target) => target.skinID == skinID && target.skinTYPE == skinTYPE);
		return skinData.avatar;
	}

	public void OpenCollectible()
	{
		if (!openCollectible)
		{
			openCollectible = true;
			openAnimate.Play(animateClip);
			StartCoroutine(OpenAnimate());
		}
	}

	private IEnumerator OpenAnimate()
	{
		yield return waitForSeconds;
		worldMap.CloseWorldMap();
		graphic.ShowPopup(value: true, Graphic.Dialog.Collectible);
	}

	public void CloseCollectible()
	{
		openCollectible = false;
		worldRectTransform.localScale = Vector3.one;
		worldRectTransform.anchoredPosition = Vector2.zero;
		graphic.ShowPopup(value: false, Graphic.Dialog.Collectible);
	}
}
